package com.example.administrator.daydayappmarket;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.example.administrator.daydayappmarket.game.fragment.GameFragment;
import com.example.administrator.daydayappmarket.list.fragment.ListFragment;
import com.example.administrator.daydayappmarket.personal.fragment.PersonalFragment;
import com.example.administrator.daydayappmarket.recommend.fragment.RecommendFragment;
import com.example.administrator.daydayappmarket.software.fragment.SoftwareFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @InjectView(R.id.activity_main_RadioGroup)
    RadioGroup activityMainRadioGroup;
    @InjectView(R.id.activity_main_download)
    RelativeLayout download;
    @InjectView(R.id.activity_edit_search)
    EditText search;
    @InjectView(R.id.activity_main_FragmentLayout)
    FrameLayout activityMainFragmentLayout;
    @InjectView(R.id.activity_main)
    RelativeLayout activityMain;
    @InjectView(R.id.activity_main_LinearLayout_top)
    LinearLayout activityMainLinearLayoutTop;
    private Fragment[] fragments;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        initFragment();

        activityMainRadioGroup.setOnCheckedChangeListener(this);

        ((RadioButton) activityMainRadioGroup.getChildAt(0)).setChecked(true);
    }

    private void initFragment() {
        fragments = new Fragment[5];
        manager = getSupportFragmentManager();
        fragments[0] = new RecommendFragment();
        fragments[1] = new SoftwareFragment();
        fragments[2] = new GameFragment();
        fragments[3] = new ListFragment();
        fragments[4] = new PersonalFragment();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        FragmentTransaction fragmentTransaction = manager.beginTransaction();

        switch (checkedId) {
            case R.id.activity_main_RadioButton_recommend:
                activityMainLinearLayoutTop.setVisibility(View.VISIBLE);
                fragmentTransaction.replace(R.id.activity_main_FragmentLayout, fragments[0]);
                break;
            case R.id.activity_main_RadioButton_software:
                activityMainLinearLayoutTop.setVisibility(View.VISIBLE);
                fragmentTransaction.replace(R.id.activity_main_FragmentLayout, fragments[1]);
                break;
            case R.id.activity_main_RadioButton_game:
                activityMainLinearLayoutTop.setVisibility(View.VISIBLE);
                fragmentTransaction.replace(R.id.activity_main_FragmentLayout, fragments[2]);
                break;
            case R.id.activity_main_RadioButton_list:
                activityMainLinearLayoutTop.setVisibility(View.VISIBLE);
                fragmentTransaction.replace(R.id.activity_main_FragmentLayout, fragments[3]);
                break;
            case R.id.activity_main_RadioButton_personal:
                activityMainLinearLayoutTop.setVisibility(View.GONE);
                fragmentTransaction.replace(R.id.activity_main_FragmentLayout, fragments[4]);
                break;
        }
        fragmentTransaction.commit();

    }

    @OnClick({R.id.activity_edit_search, R.id.activity_main_download})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_edit_search:
                Intent intent = new Intent(this,SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_main_download:
                Intent intent1 = new Intent(this,DownloadActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
